package com.icss.mqsp.db;

import com.ibatis.sqlmap.client.SqlMapClient;
import com.icss.pangu.util.string.StringUtil;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class DBDataProvider
  implements DataPrivoder
{
  private static Log log = LogFactory.getLog(DBDataProvider.class);
  
  public Object getData(String dataSetName, Map<String, String> condition)
  {
    return getDataForList(dataSetName, condition);
  }
  
  public List getDataForList(String dataSetName, Map<String, String> condition)
  {
    List data = null;
    
    String systemName = "";
    String sqlId = dataSetName;
    String[] dataSetNames = StringUtil.convertToArray(dataSetName, ".");
    if (dataSetNames.length > 1) {
      systemName = dataSetNames[0];
    }
    DBClient dbConnect = DBClient.getInstance();
    SqlMapClient sqlMapClient = dbConnect.getSqlMapClient(systemName);
    if (condition == null) {
      condition = new HashMap();
    }
    try
    {
      data = sqlMapClient.queryForList(sqlId, condition);
    }
    catch (Exception ex)
    {
      ex.printStackTrace();
      log.error("读取数据" + dataSetName + "失败！");
      log.error(ex);
    }
    return data;
  }
  
  public List getParamsDefine(String dataSetName)
  {
    return null;
  }
  
  public void insert(String sql, String dataSetName)
  {
    try
    {
      DBClient dbConnect = DBClient.getInstance();
      SqlMapClient sqlMapClient = dbConnect.getSqlMapClient("");
      sqlMapClient.insert("insertFin", sql);
    }
    catch (Exception e)
    {
      log.error(e.toString());
      e.printStackTrace();
    }
  }
  
  public int insert(String dataSetName, Map<String, String> condition)
  {
    String systemName = "";
    String sqlId = dataSetName;
    String[] dataSetNames = StringUtil.convertToArray(dataSetName, ".");
    if (dataSetNames.length > 1)
    {
      systemName = dataSetNames[0];
      sqlId = dataSetNames[1];
    }
    DBClient dbConnect = DBClient.getInstance();
    SqlMapClient sqlMapClient = dbConnect.getSqlMapClient(systemName);
    if (condition == null) {
      condition = new HashMap();
    }
    int ret = 0;
    try
    {
      sqlMapClient.insert(sqlId, condition);
      ret = 1;
    }
    catch (SQLException e)
    {
      int ret1;
      e.printStackTrace();
      log.error(e);
      ret1 = -1;
    }
    return ret;
  }
  
  public int insertAll(String dataSetName, List list)
  {
    String systemName = "";
    String sqlId = dataSetName;
    String[] dataSetNames = StringUtil.convertToArray(dataSetName, ".");
    if (dataSetNames.length > 1)
    {
      systemName = dataSetNames[0];
      sqlId = dataSetNames[1];
    }
    DBClient dbConnect = DBClient.getInstance();
    SqlMapClient sqlMapClient = dbConnect.getSqlMapClient(systemName);
    int ret = 0;
    try
    {
      sqlMapClient.startTransaction();
      sqlMapClient.startBatch();
      for (int i = 0; i < list.size(); i++)
      {
        Map map = (Map)list.get(i);
        sqlMapClient.insert(sqlId, map);
      }
      sqlMapClient.executeBatch();
      sqlMapClient.commitTransaction();
      ret = 1;
    }
    catch (SQLException e)
    {
      int ret1;
      e.printStackTrace();
      log.error(e);
      int ret11 = -1;
      try
      {
        sqlMapClient.endTransaction();
      }
      catch (SQLException e1)
      {
        e1.printStackTrace();
      }
    }
    finally
    {
      try
      {
        sqlMapClient.endTransaction();
      }
      catch (SQLException e)
      {
        e.printStackTrace();
      }
    }
    return ret;
  }
  
  public List select(String dataSetName, Map<String, String> condition)
  {
    String systemName = "";
    String sqlId = dataSetName;
    String[] dataSetNames = StringUtil.convertToArray(dataSetName, ".");
    if (dataSetNames.length > 1)
    {
      systemName = dataSetNames[0];
      sqlId = dataSetNames[1];
    }
    DBClient dbConnect = DBClient.getInstance();
    SqlMapClient sqlMapClient = dbConnect.getSqlMapClient(systemName);
    List list = new ArrayList();
    try
    {
      list = sqlMapClient.queryForList(sqlId, condition);
    }
    catch (SQLException e)
    {
      log.error(e);
    }
    return list;
  }
  
  public int del(String dataSetName, Map<String, String> condition)
  {
    String systemName = "";
    String sqlId = dataSetName;
    String[] dataSetNames = StringUtil.convertToArray(dataSetName, ".");
    if (dataSetNames.length > 1)
    {
      systemName = dataSetNames[0];
      sqlId = dataSetNames[1];
    }
    DBClient dbConnect = DBClient.getInstance();
    SqlMapClient sqlMapClient = dbConnect.getSqlMapClient(systemName);
    if (condition == null) {
      condition = new HashMap();
    }
    int ret = 0;
    try
    {
      sqlMapClient.delete(sqlId, condition);
      ret = 1;
    }
    catch (SQLException e)
    {
      int ret1;
      log.error(e);
      ret1 = -1;
    }
    return ret;
  }
  
  public int delAll(String dataSetName, List list)
  {
    String systemName = "";
    String sqlId = dataSetName;
    String[] dataSetNames = StringUtil.convertToArray(dataSetName, ".");
    if (dataSetNames.length > 1)
    {
      systemName = dataSetNames[0];
      sqlId = dataSetNames[1];
    }
    DBClient dbConnect = DBClient.getInstance();
    SqlMapClient sqlMapClient = dbConnect.getSqlMapClient(systemName);
    int ret = 0;
    try
    {
      sqlMapClient.startTransaction();
      sqlMapClient.startBatch();
      for (int i = 0; i < list.size(); i++)
      {
        Map map = (Map)list.get(i);
        sqlMapClient.delete(sqlId, map);
      }
      sqlMapClient.executeBatch();
      sqlMapClient.commitTransaction();
      ret = 1;
    }
    catch (SQLException e)
    {
      int ret1;
      log.error(e);
      int ret11 = -1;
      try
      {
        sqlMapClient.endTransaction();
      }
      catch (SQLException s)
      {
        s.printStackTrace();
      }
    }
    finally
    {
      try
      {
        sqlMapClient.endTransaction();
      }
      catch (SQLException e)
      {
        e.printStackTrace();
      }
    }
    return ret;
  }
  
  public int update(String dataSetName, Map<String, String> condition)
  {
    String systemName = "";
    String sqlId = dataSetName;
    String[] dataSetNames = StringUtil.convertToArray(dataSetName, ".");
    if (dataSetNames.length > 1)
    {
      systemName = dataSetNames[0];
      sqlId = dataSetNames[1];
    }
    DBClient dbConnect = DBClient.getInstance();
    SqlMapClient sqlMapClient = dbConnect.getSqlMapClient(systemName);
    if (condition == null) {
      condition = new HashMap();
    }
    int ret = 0;
    try
    {
      sqlMapClient.update(sqlId, condition);
      ret = 1;
    }
    catch (SQLException e)
    {
      int ret1;
      log.error(e);
      ret1 = -1;
    }
    return ret;
  }
}
